package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.UserMannagementDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = UserMannagementService.class)
@Transactional
public class UserMannagementServiceImpl implements UserMannagementService {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserMannagementDao userMannagementDao;
    //新增检查组，同时需要关联检查项（设置多对多关系）
    public void add(User user, Integer[] roleIds) {
        //        dataSource.getConnection();
        //新增检查组
        userMannagementDao.add(user);
        Integer userId = user.getId();//检查组id
        //设置检查组和检查项多对多关系
        this.setUserAndRole(userId,roleIds);
    }

    //分页查询
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = userMannagementDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public User findById(Integer id) {
        return userMannagementDao.findById(id);
    }

    //编辑检查项信息，同时需要设置关联关系
    public void edit(User user, Integer[] roleIds) {
        //基本信息修改
        userMannagementDao.edit(user);
        //清理检查组和检查项的关联关系（操作中间关系表）
        userMannagementDao.deleteByUserAndRole(user.getId());
        //重新建立检查组和检查项的关联关系
        this.setUserAndRole(user.getId(),roleIds);
    }

    public List<User> findAll() {
        return userMannagementDao.findAll();
    }

    //设置检查组和检查项多对多关系
    public void setUserAndRole(Integer userId,Integer[] roleIds){
        //设置多对多关系
        if(roleIds != null && roleIds.length > 0){
            for (Integer roleId : roleIds) {//检查项id
                Map<String,Integer> map = new HashMap<>();
                map.put("userId",userId);
                map.put("roleId",roleId);
                userMannagementDao.setUserAndRole(map);
            }
        }
    }
    public List<Role> roleFindAll() {
        return userMannagementDao.roleFindAll();
    }

    //根据检查组id查询关联的检查项id
    public List<Integer> findRoleIdsByUserId(Integer userId) {
        return userMannagementDao.findRoleIdsByUserId(userId);
    }


    //删除用户先删除关联表
    @Override
    public void deleteByUserId(Integer userId) {
        userMannagementDao.deleteByUserAndRole(userId);
        userMannagementDao.deleteByUserId(userId);
    }

    @Override
    public int findUserByUsername(String username) {
        //判断用户是否已经存在
       return userMannagementDao.findUserByUsername(username);
    }


}
